//用户授权角色
$(function(){
	
	//关闭dialog
	$('#closeuId').click(function(){
		//清空值
		userIds=[];
		roleIds = [];
		$("#uDialogId").dialog("close");
	});
	
	//角色类型授权
	$("#saveuId").click(function(){
		var node = $('#treeId').tree('getChecked');
		if(node.length==0){
			$.messager.alert('提示','请至少选一个节点',"info");
			return;
		}
		for(var i=0;i<node.length;i++){
			if(null!=node[i].id){
				roleIds.push(node[i].id);
			}
		}
		
		var url = $WEB_ROOT_PATH+'/w/wh001/setUserRole';
		$.post(url,{
			"userIds[]" : userIds,
			"roleIds[]" : roleIds
			},function(r){
			if(r=="true" || r){
				$.messager.alert('提示','操作成功',"",function(){
				});
			}else{
				$.messager.alert('提示','操作失败',"",function(){
				});
			}
		});
		
	});
	
});

/**
 * 注：全局变量
 */
var userIds = [];
var roleIds =[];

function setUserRole(){ //授权角色开始
	var selRow = $("#userListId").datagrid("getSelections");// 返回选中多行
	if(selRow.length==0 || selRow.length>1){
		$.messager.alert("提示", "请选择一行数据！","warning");
		return;
	}
	for(var i = 0;i<selRow.length;i++){
		userIds.push(selRow[i].id);
	}
	$("#uDialogId").dialog({
		closed: false,
		closable: false,
		left:'15%',
		top:'5%',
		title:'授权角色',
		iconCls:'icon-save',
		onOpen:function(){
			//根据用户id获取角色
			//复选框改为单选，故用下标取值
			$.post($WEB_ROOT_PATH+'/w/wh001/getUserRole',{id:userIds[0]},function(result){
				$("#treeId").tree({ // tree角色全部加载
					url : $WEB_ROOT_PATH + '/w/wh001/getSysRoleTree',
					lines : false,
					animate : true,
					checkbox:true,
					cascadeCheck:false, //是否级联选择
					onSelect:function(node){ //点击事件
					},
					onCheck : function(node, checked) {
						if (checked) {
							var parentNode = $("#treeId").tree('getParent', node.target);
							if (parentNode != null) {
								$("#treeId").tree('check', parentNode.target);
							}
						} else {
							var childNode = $("#treeId").tree('getChildren', node.target);
							if (childNode.length > 0) {
								for (var i = 0; i < childNode.length; i++) {
									$("#treeId").tree('uncheck', childNode[i].target);
								}
							}
						}
					},
					onLoadSuccess : function(node, data) {
						if (data) {
							$(data).each(function(index, d) {
								if(null!=result && result !=""){
									for(var i = 0;i<result.length;i++){
										if(d.id == result[i].roleid){
											var node = $('#treeId').tree('find', d.id);
											$("#treeId").tree("check",node.target);
										}
									}
								}
							});
							
						}
					}
				});
				
			},'json');
		}
	});
}